package org.wattdepot.cli.command;

import java.util.List;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Creates a summary of a source entered by the user. Displaying the name, any subsources,
 * description, owner, location, coordinates, and properties. This class extends
 * CommandLineInterface to have access to the WattDepotClient and implements WattDepotCliCommand
 * interface to ensure this class does a specific command.
 * 
 * @author Edward Meyer, Bao Huy Ung
 */
public class ListSummary extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Constructor for a ListSummary object which lists information about a specific source entered by
   * the user.
   */
  public ListSummary() {
    // Empty constructor.
  }

  /**
   * Returns a summary of a source entered by the user.
   * 
   * @param args Is a list of all the arguments entered by the user.
   * @return A string summarizing the source, or an error message.
   */
  public String doCommand(List<String> args) {

    // If there are too few or too many arguments, return an error message.
    if (args.size() != 2) {
      return "Error: Incorrect number of arguments were entered. Expected 2, instead "
          + args.size() + " were entered.";
    }

    // String variable that will hold all the information.
    StringBuffer output = new StringBuffer(200);

    String inputSourceName = args.get(1);
    Source printSource = null;
    printSource = isValidSource(inputSourceName);
    if (printSource == null) {
      return "\"" + inputSourceName + "\" is not a valid source.";
    }

    output.append("SubSources: ");
    // Check if the source has any subsources.
    if (printSource.isSetSubSources()) {
      // Print every subsource of the given source.
      for (String subsource : printSource.getSubSources().getHref()) {
        // Print only the name of the source, minus the URL address.
        output.append(subsource.substring(subsource.indexOf("SIM_")));
        output.append(", ");
      }
      // Chop off the last comma.
      output.delete(output.lastIndexOf(","), output.length() - 1);
      output.append(cr);
    }
    else {
      // There are no subsources for the given source.
      output.append("None");
      output.append(cr);
    }

    // Print the description, owner, location, and coordinates.
    output.append("Description: ");
    output.append(printSource.getDescription());
    output.append(cr);
    output.append("Owner: ");
    output.append(printSource.getOwner().substring(printSource.getOwner().indexOf("/users/") + 7));
    output.append(cr);
    output.append("Location: ");
    output.append(printSource.getLocation());
    output.append(cr);
    output.append("Coordinates: ");
    output.append(printSource.getCoordinates());
    output.append(cr);

    output.append("Properties: ");
    // Check if the given source has properties.
    if (printSource.isSetProperties()) {
      // Print every property of the given source.
      for (Property property : printSource.getProperties().getProperty()) {
        output.append('(');
        output.append(property.getKey());
        output.append(" : ");
        output.append(property.getValue());
        output.append(") ");
      }
      output.append(cr);
    }
    else {
      output.append("None");
      output.append(cr);
    }

    // Print the source summary for first, last, and total sensor data.
    SourceSummary printSummary = null;
    try {
      printSummary = client.getSourceSummary(inputSourceName);
    }
    catch (Exception e) {
      return "An error occured while trying to retrieve data about the source's summary.\n" + e;
    }
    output.append("Earliest data: ");
    output.append(printSummary.getFirstSensorData());
    output.append(cr);
    output.append("Latest data: ");
    output.append(printSummary.getLastSensorData());
    output.append(cr);
    output.append("Total data points: ");
    output.append(printSummary.getTotalSensorDatas());

    return output.toString();
  }

}